using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_searchresult : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private Tools t = new Tools();
	private int page;
	private int currentpage;
	private int type;
	private bool loggedin = false;

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		string target = "";
		Account acc = new Account();

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			acc = ap.GetAccountBySession(Session["Zappr"].ToString());
			loggedin = true;
		}

		// type
		if(Request.QueryString.Get("type") == null) {
			type = 0;
		}
		else {
			type = Convert.ToInt32(Request.QueryString.Get("type").ToString());
		}

		// pages
		if(Request.QueryString.Get("page") == null) {
			page = 0;
			currentpage = 1;
		}
		else {
			page = Convert.ToInt32(Request.QueryString.Get("page").ToString());
			currentpage = page;
			page = t.ConvertPageToStartValue(page);
		}

		// target
		if(Request.QueryString.Get("target") == null) {
			target = "a";
		}
		else {
			if(loggedin) {
				target = Request.QueryString.Get("target").ToString();
			}
			else {
				target = "a";
			}
		}

		// query
		switch(Request.QueryString.GetKey(0)) {
			case "q":
				this.lblTitle.Text += " " + GetLocalResourceObject("Text").ToString();
				this.lblTitle.Text += ": " + Request.QueryString.Get("q").ToString();
				this._SearchNormal(target, type, Request.QueryString.Get("q").ToString());
				break;

			case "tag":
				this.lblTitle.Text += " " + GetLocalResourceObject("Tag").ToString();
				this.lblTitle.Text += ": " + Request.QueryString.Get("tag").ToString();
				this._SearchTag(target, type, Request.QueryString.Get("tag").ToString());
				break;

			case "cam":
				this.lblTitle.Text += " " + GetLocalResourceObject("Cam").ToString();
				this.lblTitle.Text += ": " + Request.QueryString.Get("cam").ToString();
				this._SearchCam(target, type, Request.QueryString.Get("cam").ToString());
				break;

			default:
				Label lblAmount = new Label();
				lblAmount.Text = "<br /><br />";
				lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
				lblAmount.Text += " 0";
				lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
				lblAmount.Text += "!";
				lblAmount.Text += "<br /><br />";

				plcResultInfo.Controls.Add(lblAmount);
				break;
		}
	}

	/// <summary>
	/// Search for query
	/// </summary>
	/// <param name="target"></param>
	/// <param name="q"></param>
	private void _SearchNormal(string target, int type, string q) {
		if(q.Trim() != "") {
			int amount = 0;
			Account acc = new Account();
			ArrayList items = null;

			if(target == "a") {
				amount = ip.GetImageAmount(q, type, true);
			}
			else if(target == "y") {
				acc = ap.GetAccountBySession(Session["Zappr"].ToString());
				amount = ip.GetImageAmount(acc.ID, type, q);
			}

			Label lblAmount = new Label();
			lblAmount.Text = "<br /><br />";
			lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
			lblAmount.Text += " " + amount.ToString();

			if(amount != 1) {
				lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
			}
			else {
				lblAmount.Text += " " + GetLocalResourceObject("Photo").ToString();
			}

			lblAmount.Text += "!";
			lblAmount.Text += "<br /><br />";

			plcResultInfo.Controls.Add(lblAmount);

			switch(target) {
				case "a":
					items = ip.GetImageListByParam(q, type, page, page + 20);
					break;
				
				case "y":
					items = ip.GetImageListByParam(q, type, page, page + 20, acc.ID);
					break;

				default:
					items = ip.GetImageListByParam(q, type, page, page + 20);
					break;
			}

			if(items != null) {
				foreach(ImageItem item in items) {
					this.DisplayRow(item);
				}

				// footer
				if(amount > 19 || page > 0) {
					this.DisplayPageList("q", q, target, type, amount);
				}
			}
			else {
				Label lblAmount2 = new Label();
				lblAmount2.Text = "<br /><br />";
				lblAmount2.Text += GetLocalResourceObject("WeVeFound").ToString();
				lblAmount2.Text += " 0";
				lblAmount2.Text += " " + GetLocalResourceObject("Photos").ToString();
				lblAmount2.Text += "!";
				lblAmount2.Text += "<br /><br />";

				plcResultInfo.Controls.Add(lblAmount2);
			}
		}
		else {
			Label lblAmount = new Label();
			lblAmount.Text = "<br /><br />";
			lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
			lblAmount.Text += " 0";
			lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
			lblAmount.Text += "!";
			lblAmount.Text += "<br /><br />";

			plcResultInfo.Controls.Add(lblAmount);
		}
	}

	/// <summary>
	/// Search for tags
	/// </summary>
	/// <param name="target"></param>
	/// <param name="q"></param>
	private void _SearchTag(string target, int type, string q) {
		if(q.Trim() != "") {
			int amount = 0;
			Account acc = new Account();
			List<ImageItem> items = null;

			if(target == "a") {
				amount = ip.GetImageAmountByTag(q, type);
			}
			else if(target == "y") {
				acc = ap.GetAccountBySession(Session["Zappr"].ToString());
				amount = ip.GetImageAmountByTag(acc.ID, q, type);
			}

			Label lblAmount = new Label();
			lblAmount.Text = "<br /><br />";
			lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
			lblAmount.Text += " " + amount.ToString();

			if(amount != 1) {
				lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
			}
			else {
				lblAmount.Text += " " + GetLocalResourceObject("Photo").ToString();
			}

			lblAmount.Text += "!";
			lblAmount.Text += "<br /><br />";

			plcResultInfo.Controls.Add(lblAmount);

			// get photos
			if(target == "a") {
				items = ip.GetImageListByTag(q, type, page, page + 20);
			}
			else if(target == "y") {
				items = ip.GetImageListByTag(q, type, page, page + 20, acc.ID);
			}

			// list photos
			if(items != null) {
				foreach(ImageItem item in items) {
					this.DisplayRow(item);
				}

				// footer
				if(amount > 19 || page > 0) {
					this.DisplayPageList("tag", q, target, type, amount);
				}
			}
			else {
				Label lblAmount2 = new Label();
				lblAmount2.Text = "<br /><br />";
				lblAmount2.Text += GetLocalResourceObject("WeVeFound").ToString();
				lblAmount2.Text += " 0";
				lblAmount2.Text += " " + GetLocalResourceObject("Photos").ToString();
				lblAmount2.Text += "!";
				lblAmount2.Text += "<br /><br />";

				plcResultInfo.Controls.Add(lblAmount2);
			}
		}
		else {
			Label lblAmount = new Label();
			lblAmount.Text = "<br /><br />";
			lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
			lblAmount.Text += " 0";
			lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
			lblAmount.Text += "!";
			lblAmount.Text += "<br /><br />";

			plcResultInfo.Controls.Add(lblAmount);
		}
	}

	/// <summary>
	/// Search for cams
	/// </summary>
	/// <param name="target"></param>
	/// <param name="q"></param>
	private void _SearchCam(string target, int type, string cam) {
		if(cam.Trim() != "") {
			int amount = 0;
			Account acc = new Account();
			List<ImageItem> items = new List<ImageItem>();
			
			// get photos
			try {
				switch(target) {
					case "a":
						amount = this.ip.GetImageAmountByCam(cam, type, loggedin);
						items = this.ip.GetImageListByCam(
							cam, type, page, page + 20, loggedin
						);
						break;

					case "y":
						acc = ap.GetAccountBySession(Session["Zappr"].ToString());
						amount = this.ip.GetImageAmountByCam(acc.ID, cam, type, loggedin);
						items = this.ip.GetImageListByCam(
							cam, type, page, page + 20, acc.ID, loggedin
						);
						break;

					default:
						amount = this.ip.GetImageAmountByCam(cam, type, loggedin);
						items = this.ip.GetImageListByCam(
							cam, type, page, page + 20, loggedin
						);
						break;
				}
			}
			catch(Exception ex) {
				string s = ex.Message;
				amount = 0;
			}

			Label lblAmount = new Label();
			lblAmount.Text = "<br /><br />";
			lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
			lblAmount.Text += " " + amount.ToString();

			if(amount != 1) {
				lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
			}
			else {
				lblAmount.Text += " " + GetLocalResourceObject("Photo").ToString();
			}

			lblAmount.Text += "!";
			lblAmount.Text += "<br /><br />";

			plcResultInfo.Controls.Add(lblAmount);

			// list photos
			if(items != null) {
				foreach(ImageItem item in items) {
					this.DisplayRow(item, target, type, true);
				}

				// footer
				if(amount > 19 || page > 0) {
					this.DisplayPageList("cam", cam, target, type, amount);
				}
			}
			else {
				Label lblAmount2 = new Label();
				lblAmount2.Text = "<br /><br />";
				lblAmount2.Text += GetLocalResourceObject("WeVeFound").ToString();
				lblAmount2.Text += " 0";
				lblAmount2.Text += " " + GetLocalResourceObject("Photos").ToString();
				lblAmount2.Text += "!";
				lblAmount2.Text += "<br /><br />";

				plcResultInfo.Controls.Add(lblAmount2);
			}
		}
		else {
			Label lblAmount = new Label();
			lblAmount.Text = "<br /><br />";
			lblAmount.Text += GetLocalResourceObject("WeVeFound").ToString();
			lblAmount.Text += " 0";
			lblAmount.Text += " " + GetLocalResourceObject("Photos").ToString();
			lblAmount.Text += "!";
			lblAmount.Text += "<br /><br />";

			plcResultInfo.Controls.Add(lblAmount);
		}
	}

	/// <summary>
	/// Display the images
	/// </summary>
	/// <param name="item"></param>
	public void DisplayRow(ImageItem item) {
		this.DisplayRow(item, "", 0, false);
	}

	/// <summary>
	/// Display the images
	/// </summary>
	/// <param name="item"></param>
	public void DisplayRow(ImageItem item, string target, int type, bool withCamInfo) {
		string imgtitle = item.Name;

		if(imgtitle.Length > 21) {
			if(imgtitle.IndexOf(' ') == -1 || imgtitle.IndexOf(' ') > 20) {
				imgtitle = item.Name.Substring(0, 19) + "...";
			}
		}

		// image
		HyperLink link = new HyperLink();
		link.ImageUrl = "../../Images/" + item.Image240;
		link.CssClass = "listSquareThumbnail";
		//link.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
		link.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

		Panel p1 = new Panel();
		p1.CssClass = "searchBoxLeft";
		p1.Controls.Add(link);

		// info
		Label lblTitle = new Label();
		lblTitle.CssClass = "listTitle";
		lblTitle.Text = imgtitle;

		Panel p2 = new Panel();
		p2.CssClass = "searchBoxRight";
		p2.Controls.Add(lblTitle);

		// cam
		if(withCamInfo) {
			Label lblCam = new Label();
			lblCam.CssClass = "listText";
			lblCam.Text = "<br /><br />";
			lblCam.Text += GetLocalResourceObject("UsedCam").ToString();
			lblCam.Text += ": ";

			p2.Controls.Add(lblCam);

			HyperLink cam = new HyperLink();
			cam.CssClass = "listText";
			cam.Text = item.EquipmentModel;
			cam.NavigateUrl = "~/search.aspx?cam=" + item.EquipmentModel;
			cam.NavigateUrl += "&target=" + target + "&type=" + type.ToString();

			p2.Controls.Add(cam);
			p2.Controls.Add(new LiteralControl("<br />"));

			Label lblMaker = new Label();
			lblMaker.CssClass = "listText";
			lblMaker.Text = GetLocalResourceObject("Maker").ToString();
			lblMaker.Text += ": ";

			p2.Controls.Add(lblMaker);

			HyperLink cam2 = new HyperLink();
			cam2.CssClass = "listText";
			cam2.Text = item.EquipmentMaker;
			cam2.NavigateUrl = "~/search.aspx?cam=" + item.EquipmentMaker;
			cam2.NavigateUrl += "&target=" + target + "&type=" + type.ToString();

			p2.Controls.Add(cam2);
		}
		else {
			p2.Controls.Add(new LiteralControl("<br />"));
		}

		// user
		Label lblUser = new Label();
		lblUser.CssClass = "listText";
		lblUser.Text = "<br />";
		lblUser.Text += GetLocalResourceObject("ShotBy").ToString();
		lblUser.Text += " ";

		p2.Controls.Add(lblUser);

		HyperLink user = new HyperLink();
		user.CssClass = "listText";
		user.Text = item.User.Username;
		//user.NavigateUrl = "~/default.aspx?id=images&user=" + item.User.Username;
		user.NavigateUrl = "~/images/" + item.User.Username + "/default.aspx";

		p2.Controls.Add(user);

		Label lblDate = new Label();
		lblDate.CssClass = "listText";
		lblDate.Text = " ";
		lblDate.Text += GetLocalResourceObject("At").ToString();
		lblDate.Text += " ";
		lblDate.Text += item.UploadDate.ToLongDateString();

		p2.Controls.Add(lblDate);

		Label lblbr = new Label();
		lblbr.Text = "<br /><br />";

		p2.Controls.Add(lblbr);

		// tags
		if(item.Tags.Count > 0) {
			Image imgtag = new Image();
			imgtag.ImageUrl = "~/App_Themes/Default/SilkIcons/tag.png";

			p2.Controls.Add(imgtag);

			Label lblnb = new Label();
			lblnb.Text = "&nbsp;";

			p2.Controls.Add(lblnb);

			foreach(TagItem tag in item.Tags) {
				HyperLink linktag = new HyperLink();
				linktag.CssClass = "listText";
				//linktag.NavigateUrl = "~/default.aspx?id=images&user="
				//	+ item.User.Username
				//	+ "&tag=" + tag.Tag;
				linktag.NavigateUrl = "~/images/"
					+ item.User.Username
					+ "/tag/" + tag.Tag
					+ "/default.aspx";
				linktag.Text = tag.Tag;

				p2.Controls.Add(linktag);

				Label lbln = new Label();
				lbln.Text = "&nbsp;";

				p2.Controls.Add(new LiteralControl(" "));
			}
		}

		// result line
		HtmlTableRow row = new HtmlTableRow();

		HtmlTableCell cell = new HtmlTableCell();
		cell.VAlign = "top";
		cell.Width = "600";
		cell.Style.Add("width", "600px");
		cell.Style.Add("padding-bottom", "20px");

		// master panel
		Panel master = new Panel();
		master.CssClass = "plcImageList";
		master.Controls.Add(p1);
		master.Controls.Add(p2);

		//plcResult.Controls.Add(master);

		cell.Controls.Add(master);
		row.Cells.Add(cell);
		imageTable.Rows.Add(row);
	}

	/// <summary>
	/// Display the pages on the footer
	/// </summary>
	/// <param name="amount"></param>
	private void DisplayPageList(string query, string word, string target, int type, int amount) {
		long pages = (Convert.ToInt64(amount) / 20) + 1;
		int counter = 0;

		// pages
		HyperLink link = null;

		if(pages > 1) {
			plcFooter.Controls.Add(new LiteralControl("<br />"));

			if(currentpage != 1) {
				link = null;
				link = new HyperLink();
				link.CssClass = "pageStep";
				link.NavigateUrl = "~/search.aspx?" + query + "=" + word;
				link.NavigateUrl += "&target=" + target;
				link.NavigateUrl += "&type=" + type.ToString();
				link.NavigateUrl += "&page=" + (currentpage - 1).ToString();
				link.Text = GetLocalResourceObject("Previous").ToString();

				plcFooter.Controls.Add(link);
			}

			for(int i = 1; i <= pages; i++) {
				link = new HyperLink();

				if(i == currentpage) {
					link.CssClass = "pageCurrentStep";
				}
				else {
					link.CssClass = "pageStep";
				}

				link.NavigateUrl = "~/search.aspx?" + query + "=" + word;
				link.NavigateUrl += "&target=" + target;
				link.NavigateUrl += "&type=" + type.ToString();
				link.NavigateUrl += "&page=" + i.ToString();
				link.Text = i.ToString();

				// aktiver link ist "page"

				plcFooter.Controls.Add(link);

				plcFooter.Controls.Add(new LiteralControl("&nbsp;"));

				link = null;
				counter++;
			}

			if(currentpage < counter) {
				link = null;
				link = new HyperLink();
				link.CssClass = "pageStep";
				link.NavigateUrl = "~/search.aspx?" + query + "=" + word;
				link.NavigateUrl += "&target=" + target;
				link.NavigateUrl += "&type=" + type.ToString();
				link.NavigateUrl += "&page=" + (currentpage + 1).ToString();
				link.Text = GetLocalResourceObject("Next").ToString();

				plcFooter.Controls.Add(link);
			}
		}
	}
}
